<template>
  <view class="search_bgc">
    <uni-search-bar
      class="search"
      @confirm="search"
      @clear="close"
      @cancel="close"
      placeholder="搜索"
      v-model="inputClearValue"
      @input="change"
    >
    </uni-search-bar>

    <!-- 搜索历史 -->
    <view class="history-box" v-show="searchResults.length === 0">
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag
          @click="search_history(i)"
          :text="item"
          inverted
          circle
          type="success"
          v-for="(item, i) in historyList"
          :key="i"
        ></uni-tag>
      </view>
    </view>

    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-show="searchResults.length > 0">
      <view
        class="sugg-item"
        v-for="(item, i) in searchResults"
        :key="i"
        @click="gotoDetail(item.goods_id)"
      >
        <view class="goods-name">{{ item.goods_name }}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
  </view>
</template>

<script>
import { searchGoodsListAPI } from "@/api/goods_list";
import { mapGetters, mapMutations } from "vuex";
export default {
  data() {
    return {
      showClearIcon: false,
      inputClearValue: "",
      searchResults: [],
      // 搜索记录
      historyList: [],
    };
  },
  computed: {
    ...mapGetters(["history"]),
  },
  onLoad() {
    this.historyList = this.history;
  },
  methods: {
    ...mapMutations(["getHistory"]),

    async search() {
      const res = await searchGoodsListAPI({ query: this.inputClearValue });
      this.searchResults = res.message;
      // 保存浏览记录
      this.saveHistory();

      this.getHistory(this.historyList);
    },
    saveHistory() {
      const set = new Set(this.historyList);
      set.delete(this.inputClearValue);
      set.add(this.inputClearValue);
      this.historyList = Array.from(set);
    },
    gotoDetail(id) {
      wx.navigateTo({
        url: "/pages/goods_detail/index?goods_id=" + id,
      });
    },
    close() {
      this.searchResults = [];
    },
    change() {
      if (this.inputClearValue === "") {
        this.searchResults = [];
      }
    },
    // 清空历史记录
    clearHistory() {
      this.historyList = [];
      this.getHistory(this.historyList);
    },
    // 根据历史记录查询
    search_history(index) {
      this.inputClearValue = this.historyList[index];
      this.search();
    },
  },
};
</script>

<style lang="scss" scoped>
.search_bgc {
  background-color: #eb4450;
  width: 100%;
}
.history-box {
  padding: 0 5px;
  background-color: #f0f0f0;

  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;

    .uni-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}

.sugg-list {
  padding: 0 5px;
  background-color: #f0f0f0;

  .sugg-item {
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      // 文字不允许换行（单行文本）
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用 ... 代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}
</style>